package com.hsnn.medstgmini.sys.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.sys.dao.RegHealthbureauDao;
import com.hsnn.medstgmini.sys.model.AuditType;
import com.hsnn.medstgmini.sys.model.RegHealthbureau;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.PinYin;

@Repository
public class RegHealthbureauDaoImp extends BaseHibernateDAO implements
		RegHealthbureauDao {
	@Autowired
	private HibernateSqlUtil dao;

	/**
	 * @category author 韩守松
	 * @date 2015年11月2日
	 * @param @param regHealthbureau
	 * @param @return
	 */
	public void saveRegheaBurInfo(RegHealthbureau regHealthbureau) {
		regHealthbureau.setHeaBurId(UUID.randomUUID().toString());
		getSession().saveOrUpdate(regHealthbureau);
		getSession().flush();
	}

	/**
	 * @category 获取审核卫生局信息列表
	 * @author 韩守松
	 * @date 2015年11月3日
	 * @param @param page
	 * @param @param regHealthbureau
	 * @param @return
	 */
	@Override
	public Pagination getAuditUserList(Pagination page,
			RegHealthbureau regHealthbureau) {
		Session session = getSession();
		Criteria criteria = session.createCriteria(RegHealthbureau.class);
		criteria.add(Restrictions.eq("auditStatus", AuditType.normal.getKey()));

		// 搜索条件
		if (regHealthbureau != null) {
			String heaBurCode = regHealthbureau.getHeaBurCode();
			if (StringUtils.isNotBlank(heaBurCode)) {
				criteria.add(Restrictions.eq("heaBurCode", heaBurCode.trim()));
			}
			String heaBurName = regHealthbureau.getHeaBurName();
			if (StringUtils.isNotBlank(heaBurName)) {
				criteria.add(Restrictions.or(
						Restrictions.like("heaBurName", "%" + heaBurName.trim()
								+ "%"),
						Restrictions.eq("burSpelCode",
								PinYin.getPinYin(heaBurName))));
			}
		}

		// 查询个数
		criteria.setProjection(Projections.rowCount());
		Long records = (Long) criteria.uniqueResult();
		page.setRecords(records);

		// 查询列表
		criteria.setProjection(null);
		criteria.addOrder(Order.asc("auditStatus"));
		criteria.addOrder(Order.desc("regDatetime"));
		criteria.setFirstResult(page.getFirstResult());
		criteria.setMaxResults(page.getMaxResults());
		page.setRows(criteria.list());

		return page;
	}

	/**
	 * @category 根据卫生局注册id获取信息
	 * @author 韩守松
	 * @date 2015年11月3日
	 * @param @param heaBurId
	 * @param @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public RegHealthbureau getAuditHeaBurById(String heaBurId) {
		Session session = getSession();
		if (StringUtils.isNotBlank(heaBurId)) {
			Criteria criteria = session.createCriteria(RegHealthbureau.class);
			criteria.add(Restrictions.eq("heaBurId", heaBurId));
			List<RegHealthbureau> list = (List<RegHealthbureau>) criteria
					.list();
			if (list != null && !list.isEmpty()) {
				return list.get(0);
			}
		}
		return null;
	}

	/**
	 * @category 保存卫生局注册信息审核
	 * @author 韩守松
	 * @date 2015年11月3日
	 * @param @param regHealthbureau
	 */
	@Override
	public void saveAuditHeaBur(RegHealthbureau regHealthbureau) {
		Session session = getSession();
		if (regHealthbureau != null
				&& StringUtils.isNotBlank(regHealthbureau.getHeaBurId())) {
			RegHealthbureau hi = getAuditHeaBurById(regHealthbureau
					.getHeaBurId());
			if (hi != null) {
				session.evict(hi);
				regHealthbureau.setAuditDatetime(new Date());
				regHealthbureau.setRegDatetime(hi.getRegDatetime());
				session.saveOrUpdate(regHealthbureau);
				session.flush();
			}
		}

	}

	/**
	 * @category 校验注册的卫生局在卫生局库中是否存在
	 * @author 韩守松
	 * @date 2015年11月3日
	 * @param @param regHealthbureau
	 * @param @return
	 */
	public boolean checkHeaBurExist(RegHealthbureau regHealthbureau) {
		Session session = getSession();
		if (regHealthbureau != null) {
			StringBuffer sql = new StringBuffer()
					.append("select count(*) from std_healthbureau where  hea_bur_name = :heaBurName ");
			Query query = session.createSQLQuery(sql.toString());
			query.setString("heaBurName", regHealthbureau.getHeaBurName());
			Long records = Long.parseLong(query.uniqueResult().toString());
			if (records != null && records > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @category 查询卫生局注册信息审核日志列表
	 * @author 韩守松
	 * @date 2015年11月4日
	 * @param @param page
	 * @param @param regHealthbureau
	 * @param @return
	 */
	@Override
	public Pagination getAuditUserLogList(Pagination page,
			RegHealthbureau regHealthbureau) {
		Session session = getSession();
		Criteria criteria = session.createCriteria(RegHealthbureau.class);
		criteria.add(Restrictions.or(
				Restrictions.eq("auditStatus", AuditType.unaudited.getKey()),
				Restrictions.eq("auditStatus", AuditType.audited.getKey())));

		// 搜索条件
		if (regHealthbureau != null) {
			String heaBurCode = regHealthbureau.getHeaBurCode();
			if (StringUtils.isNotBlank(heaBurCode)) {
				criteria.add(Restrictions.eq("heaBurCode", heaBurCode.trim()));
			}
			String heaBurName = regHealthbureau.getHeaBurName();
			if (StringUtils.isNotBlank(heaBurName)) {
				criteria.add(Restrictions.or(
						Restrictions.like("heaBurName", "%" + heaBurName.trim()
								+ "%"),
						Restrictions.eq("burSpelCode",
								PinYin.getPinYin(heaBurName))));
			}
		}

		// 查询个数
		criteria.setProjection(Projections.rowCount());
		Long records = (Long) criteria.uniqueResult();
		page.setRecords(records);

		// 查询列表
		criteria.setProjection(null);
		criteria.addOrder(Order.asc("auditStatus"));
		criteria.addOrder(Order.desc("auditDatetime"));
		criteria.setFirstResult(page.getFirstResult());
		criteria.setMaxResults(page.getMaxResults());
		page.setRows(criteria.list());

		return page;
	}
}
